﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace CorporationWars
{
    [Serializable]
    public class WorldMapControl : UIControlBase
    {
        public override int Width { get { return 512; } }
        public override int Height { get { return 256; } }

        public Color Color = Color.White;

        public UIEvent OnContinentClicked;

        ScrollableListBox<string> CountriesList;

        Vector2 listPos { get { return Position + new Vector2(0, 32); } }

        float scrollerVal = 0;

        public WorldMapControl(Game1 game) : base(game)
        {
            IsDragable = true;

            CountriesList = new ScrollableListBox<string>(Game, "Value", listPos, Width, Height - 50, "Fonts/font", false) { IsVisible = false, SaySelected = true, ForeColor = Color.LimeGreen };
            PopulateContinents();
            
        }

        protected void PopulateContinents(bool imediate = true)
        {
            ToolTipText = "The Earth...";

            CountriesList.IsVisible = false;
            ClearControls(false);

            AddControl(new UIButton(Game) { TextureAsset = "Textures/Backgrounds/World/Continents/AsiaBlack", Color = Color.SteelBlue, HoverColor = Color.LimeGreen, MouseDownColor = Color.Red, Name = "Asia", ToolTipText = "Asia", Width = this.Width, Height = this.Height, OpacityTolerance = .25f, speechRepeat = true, TipIsTutorial = false }, imediate);
            AddControl(new UIButton(Game) { TextureAsset = "Textures/Backgrounds/World/Continents/EuropeBlack", Color = Color.SteelBlue, HoverColor = Color.LimeGreen, MouseDownColor = Color.Red, Name = "Europe", ToolTipText = "Europe", Width = this.Width, Height = this.Height, OpacityTolerance = .25f, speechRepeat = true, TipIsTutorial = false }, imediate);
            AddControl(new UIButton(Game) { TextureAsset = "Textures/Backgrounds/World/Continents/AfricaBlack", Color = Color.SteelBlue, HoverColor = Color.LimeGreen, MouseDownColor = Color.Red, Name = "Africa", ToolTipText = "Africa", Width = this.Width, Height = this.Height, OpacityTolerance = .25f, speechRepeat = true, TipIsTutorial = false }, imediate);
            AddControl(new UIButton(Game) { TextureAsset = "Textures/Backgrounds/World/Continents/NorthAmericaBlack", Color = Color.SteelBlue, HoverColor = Color.LimeGreen, MouseDownColor = Color.Red, Name = "NorthAmerica", ToolTipText = "North America", Width = this.Width, Height = this.Height, OpacityTolerance = .25f, speechRepeat = true, TipIsTutorial = false }, imediate);
            AddControl(new UIButton(Game) { TextureAsset = "Textures/Backgrounds/World/Continents/SouthAmericaBlack", Color = Color.SteelBlue, HoverColor = Color.LimeGreen, MouseDownColor = Color.Red, Name = "SouthAmerica", ToolTipText = "South America", Width = this.Width, Height = this.Height, OpacityTolerance = .25f, speechRepeat = true, TipIsTutorial = false }, imediate);
            AddControl(new UIButton(Game) { TextureAsset = "Textures/Backgrounds/World/Continents/AntarticaBlack", Color = Color.SteelBlue, HoverColor = Color.LimeGreen, MouseDownColor = Color.Red, Name = "Antartica", ToolTipText = "Antartica", Width = this.Width, Height = this.Height, OpacityTolerance = .25f, speechRepeat = true, TipIsTutorial = false }, imediate);
            AddControl(new UIButton(Game) { TextureAsset = "Textures/Backgrounds/World/Continents/AustraliaBlack", Color = Color.SteelBlue, HoverColor = Color.LimeGreen, MouseDownColor = Color.Red, Name = "Australia", ToolTipText = "Australia", Width = this.Width, Height = this.Height, OpacityTolerance = .25f, speechRepeat = true, TipIsTutorial = false }, imediate);
        }

        protected void PopulateAsia(bool imediate = true)
        {
            ToolTipText = "Asia...";
            ClearControls(false);

            // Load list box with country lists.
            CountriesList.Position = listPos;
            CountriesList.IsVisible = true;
            CountriesList.Items = NameFactory.GetALLCountriesInContinent("Asia").OrderBy(c => c).ToList();
        }

        protected override void ButtonClicked(UIZone sender)
        {
            base.ButtonClicked(sender);

            scrollerVal = 0;

            switch (sender.Name)
            {
                case "Asia":
                    
                    PopulateAsia(false);
                    break;
                case "baseClose":
                    // re populate
                    PopulateContinents(false);
                    break;
            }

            if (OnContinentClicked != null)
                OnContinentClicked(sender);
        }

        public override void Update(GameTime gameTime)
        {
            scrollerVal += (Game.ScrollWheelValue * .01f);

            base.Update(gameTime);

            if (CountriesList.IsVisible)
            {
                CountriesList.Position = listPos;
                CountriesList.Update(gameTime, scrollerVal);
            }
            scrollerVal = 0;
        }

        public override void Draw(GameTime gameTime)
        {
            // Draw the BG map..
            spriteBatch.Draw(Game.Content.Load<Texture2D>("Textures/Backgrounds/World/WorldMap"), BGRectangle, Color);

            base.Draw(gameTime);

            if (CountriesList.IsVisible)
                CountriesList.Draw(gameTime);
        }        
    }
}
